import pandas as pd
import visdom
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from _datetime import datetime
mpl.rcParams['font.family'] = 'STKAITI' #'STKAITI'——字体
plt.rcParams['axes.unicode_minus'] = False   # 解决坐标轴负数的负号显示问题
vis = visdom.Visdom(env='rl_train_info')
def plot_rader():
    labels = np.array(['平均任务速度', '超时率', '平均任务时延', '总代价'])
    num_vars = len(labels)
    angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False).tolist()
    angles += angles[:1]

    fig, ax = plt.subplots(subplot_kw=dict(polar=True), figsize=(8, 8))

    # # 绘制雷达图
    # al2 = []
    # al1 = ['agent', 'agent', 'agent', 'agent', 'agent', 'agent', 'agent', 'agent']
    # for i in range(al1):
    #     al2.append('agent' + str(i))
    maxs = [3.5, 0.50, 6, 2.5]
    stats_list = [[3.39536,	0.042681464	,3.009208158,2.141782755],[2.43797,0.451229754,5.601139772,2.2469],[3.15247,	0.204559088,	3.30284943,	2.324222222]]
    for i,sts in enumerate(stats_list):
        for j,it in enumerate(sts):
            stats_list[i][j]=stats_list[i][j]/maxs[j]

    alths = ['Ours','Random', 'OPT']
    colors = ['green','red','black']

    for i, stats in enumerate(stats_list):
        stats = np.concatenate((stats, [stats[0]]))
        if i==0:
            tm=0.5
        else:
            tm=0.15
        ax.fill(angles, stats, alpha=tm, label=f' {alths[i]}',color=colors[i])


    # 添加标签
    ax.set_yticklabels([])
    ax.set_xticks(angles[:-1])
    ax.set_xticklabels(labels)

    # 添加图例
    ax.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1))

    plt.show()
def plot_four_d(data,xlabel):

    labels = ['Random','OPT','DLRL']
    fig, ax1 = plt.subplots()

    # 绘制第一个图形（右轴）

    for i,d in enumerate(data):
        if xlabel=='平均任务时延/s':
            if i==0:
                ax2 = ax1.twinx()
                ax2.plot([1,2,3,4,5],d,label=labels[i])
                ax2.legend()
            else:
                colors = ['','orange','green']
                ax1.plot([1, 2, 3, 4, 5], d, label=labels[i],color=colors[i])

        else:
            ax1.plot([1,2,3,4,5],d,label=labels[i])
    ax1.legend()

    ax1.set_xlabel('任务生成速率等级')
    ax1.set_ylabel(xlabel)
    plt.grid(True)
    plt.show()

def plot_four_fig():
    spd = [[1.32517, 2.12425, 2.43797, 2.5365, 2.58796], [3.34518, 3.24096, 3.15247, 3.09402, 3.07437],
            [3.5121, 3.3968, 3.3954, 3.4039, 3.3985]]
    ovt = [[0.72554,0.5369,0.451229754,0.41324,0.3977],[0.1684,0.19196,0.204559088,0.2148,0.21495],[0.1169,0.0486,0.042681464,0.03924,0.0381]]
    td = [[2212.88326,195.65016,5.601139772,4.68224,4.40935],[3.13516,3.23902,3.30284943,3.36172,3.3786],[3.07808,3.03366,3.009208158,2.99152,2.99185]]
    cost = [[6.199711111,3.277211111,2.2469,1.7054,1.375611111],[6.356233333,3.403580074,2.324222222,1.749277778,1.406411111],[6.637477778,3.206055556,2.141782755,1.591477778,1.271866667]]
    datas =[spd,ovt,td,cost]
    xlabels = ['平均任务速度','超时率','平均任务时延/s','总代价']
    for i,d in enumerate(datas):
        plot_four_d(d,xlabels[i])

def plot_alpha():
    alphas = [1.2,1.7,2.2,2.7,3.2]
    td =[2.20406,2.47838,3.04066,3.6098,4.05126]
    costs = [4.229833333,3.7227,3.2043,2.731266667,2.402455556]
    fig, ax1 = plt.subplots(figsize=(10, 6))
    ax1.plot(alphas, td,'o-', label='平均任务时延曲线')
    ax2 = ax1.twinx()
    ax2.plot(alphas,costs, 's-',label='总代价曲线',color='orange')
    ax1.set_ylabel('平均任务时延/s')
    ax2.set_ylabel('总代价')
    ax1.set_xlabel('终端节点奖励系数')

    plt.grid(True)
    ax2.legend()
    ax1.legend()
    plt.tight_layout()
    plt.show()
def plot_1_50_load():
    df = pd.read_csv('load_data1.csv')
    df_round_0 = df[df['回合'] == 50]
    # 筛选节点=203的数据
    df_node_203 = df_round_0[df_round_0['节点'] == 203]
    # 计算节点203的扩展平均
    df_node_203['计算负载扩展平均'] = df_node_203['计算负载'].expanding().mean()
    df_node_203['传输负载扩展平均'] = df_node_203['传输负载'].expanding().mean()*(10000/len(df_node_203['计算负载']))
    # 筛选节点=303的数据
    df_node_303 = df_round_0[df_round_0['节点'] == 303]
    # 计算节点303的扩展平均
    df_node_303['计算负载扩展平均'] = df_node_303['计算负载'].expanding().mean()
    df_node_303['传输负载扩展平均'] = df_node_303['传输负载'].expanding().mean()*(10000/len(df_node_303['计算负载']))
    # 绘制子图
    fig, axs = plt.subplots(1, 2, figsize=(10, 5))
    # 绘制节点203的子图
    axs[0].plot(df_node_203['时间片'], df_node_203['计算负载扩展平均'], label='计算负载均值')
    axs[0].plot(df_node_203['时间片'], df_node_203['传输负载扩展平均'], label='传输负载均值')
    axs[0].set_title('计算能力:7GHz   传输能力:3Mbps')
    axs[0].set_xlabel('时间片')
    axs[0].set_ylabel('负载')
    axs[0].legend()
    # 绘制节点303的子图
    axs[1].plot(df_node_303['时间片'], df_node_303['计算负载扩展平均'], label='计算负载均值')
    axs[1].plot(df_node_303['时间片'], df_node_303['传输负载扩展平均'], label='传输负载均值')
    axs[1].set_title('计算能力:3GHz   传输能力:6Mbps')
    axs[1].set_xlabel('时间片')
    axs[1].set_ylabel('负载')
    axs[1].legend()
    # 显示图表
    plt.tight_layout()
    plt.show()
def plot_1_50_load2(ep=50):
    """

    :param ep: 0或50
    :return:
    """
    df = pd.read_csv('load_data1.csv')

    # 选取回合为50的数据
    df_round_0 = df[df['回合'] == ep]

    # 筛选节点203的数据
    df_node_203 = df_round_0[df_round_0['节点'] == 203]

    # 计算节点203的扩展平均
    df_node_203['计算负载扩展平均'] = df_node_203['计算负载'].expanding().mean()
    df_node_203['传输负载扩展平均'] = df_node_203['传输负载'].expanding().mean() * (10000 / len(df_node_203['计算负载']))

    # 筛选节点303的数据
    df_node_303 = df_round_0[df_round_0['节点'] == 303]

    # 计算节点303的扩展平均
    df_node_303['计算负载扩展平均'] = df_node_303['计算负载'].expanding().mean()
    df_node_303['传输负载扩展平均'] = df_node_303['传输负载'].expanding().mean() * (10000 / len(df_node_303['计算负载']))

    # 绘制子图
    fig, axs = plt.subplots(1, 2, figsize=(10, 5))

    # 绘制节点203的子图
    axs[0].plot(df_node_203['时间片'], df_node_203['计算负载扩展平均'], label='计算负载均值')
    axs[0].set_xlabel('时间片')
    axs[0].set_ylabel('计算负载均值/G')
    axs[0].tick_params(axis='y', labelcolor='tab:blue')  # 设置计算负载坐标轴颜色为蓝色
    axs[0].set_title('计算能力:7GHz   传输能力:3Mbps')
    # 创建第二个坐标轴，并绘制传输负载均值
    axs_0_twin = axs[0].twinx()
    axs_0_twin.plot(df_node_203['时间片'], df_node_203['传输负载扩展平均'], color='orange', label='传输负载均值')
    axs_0_twin.set_ylabel('传输负载均值/Mb')
    axs_0_twin.tick_params(axis='y', labelcolor='tab:orange')  # 设置传输负载坐标轴颜色为橙色

    # 绘制节点303的子图
    axs[1].plot(df_node_303['时间片'], df_node_303['计算负载扩展平均'], label='计算负载均值')
    axs[1].set_xlabel('时间片')
    axs[1].set_ylabel('计算负载均值/G')
    axs[1].tick_params(axis='y', labelcolor='tab:blue')  # 设置计算负载坐标轴颜色为蓝色
    axs[1].set_title('计算能力:3GHz   传输能力:6Mbps')
    # 创建第二个坐标轴，并绘制传输负载均值
    axs_1_twin = axs[1].twinx()
    axs_1_twin.plot(df_node_303['时间片'], df_node_303['传输负载扩展平均'], color='orange', label='传输负载均值')
    axs_1_twin.set_ylabel('传输负载均值/Mb')
    axs_1_twin.tick_params(axis='y', labelcolor='tab:orange')  # 设置传输负载坐标轴颜色为橙色

    # 调整子图布局
    plt.subplots_adjust(wspace=0.5)
    # plt.tight_layout()

    # 显示图表
    plt.show()
def plot_rewars():
    rdata = {'PPO_Rewards': [112778.35549229076, 121251.71209599328, 128507.74415686238, 134660.47799356937, 139784.13683992124, 141801.82509238546, 147221.74912621768, 149499.8019626085, 150098.66394455178, 153597.21331754458, 155049.3761365279, 157492.65363610323, 158835.10500059812, 160931.1047650794, 163615.65753760398, 163687.47394255633, 164073.87448278992, 166534.87670065908, 165550.20373977246, 169719.31397100113, 165501.9993270616, 169045.82041251202, 168912.56568921337, 173493.0162587412, 171310.94584582036, 167676.9942030183, 168431.2285242536, 172230.72344599807, 173068.45626097653, 171017.39663114565, 171337.308341658, 172737.21367243756, 173952.24808247207, 172199.51049783494, 169621.09124486617, 169960.48751803764, 172352.11652236665, 173571.18196248167, 169223.61980519327, 173183.90985958473, 172565.0927960921, 175242.21381673834, 174158.30707070613, 174235.8560245311, 174609.78452380985, 173416.76839826652, 174722.6085137074, 175596.8361471857, 173026.23849206258, 173923.27222222192, 172722.8500000002, 175853.49332611717, 173975.31865079326, 175780.054365078, 175779.07752525207, 171976.543148518, 175497.80515872984, 176505.55039682507, 173653.78968254, 174437.8130952374, 170677.41507936534, 173928.33253968184, 174919.11944444303, 173778.74725829772, 173776.50198412695, 175516.1400793644, 174488.27727272734, 176264.8226190469, 175003.511147186, 175415.37637085133, 177085.8361111101, 175770.9051587287, 173733.8699134185, 177027.24603174586, 175713.8817460309, 173424.3253968249, 173628.76746031592, 177016.4466810958, 173989.24404761769, 174413.24682539707, 175549.38849206228, 178830.70238095225, 175620.769047619, 175421.28730158627, 176060.51706349154, 179588.82597402527, 175084.95873015744, 175394.45562770532, 174941.40555555423, 176742.35277777747, 177199.45912698357, 174842.40515872993, 175120.98571428432, 174505.75183982617, 173148.75198412707, 175936.47698412577, 174752.65158729994, 173013.94047618937, 176510.33134920645, 176096.87222222186], 'DQN_Rewards': [171444.28208304069, 185790.16661118617, 196801.9371450947, 206149.5515858484, 214028.00104782594, 217362.215203243, 224154.648077671, 226615.46431773299, 227719.06067800985, 232090.6692985878, 233357.72750035362, 236729.13799941863, 236650.0310013081, 239665.53048316672, 242591.24658271924, 242140.84267361378, 241395.02386212727, 243740.0287414423, 242957.84822748782, 247872.89073619203, 243259.7985195277, 248867.30490751597, 249161.944516259, 254507.93576922108, 252744.18086079566, 246887.28724663344, 248352.20275334732, 252905.79158118754, 254003.60377413844, 251453.0725885111, 250988.37835163882, 253577.5700793552, 255160.24578142946, 252691.72309522735, 249810.30073869735, 250712.17253967273, 253411.45634919478, 254784.20031745135, 248848.8635714183, 253538.80169107503, 252631.00415139308, 256983.3703968128, 254916.77555554482, 255679.08325395765, 255677.52595236973, 254300.2904761802, 256113.13873014736, 256938.1395237985, 252114.0246825294, 254083.19888887764, 252408.36999998926, 256966.4853174508, 253826.00103173588, 256518.519603165, 256687.57055554338, 252411.3949267312, 256292.07134919497, 257108.0108730063, 252172.63730157603, 254014.7888095132, 249535.91317459292, 254097.23158729175, 254001.56277776713, 253856.44396824398, 253486.30436506847, 256465.9081745918, 254877.20999998917, 256691.10976189337, 254966.12452379987, 255005.52801586306, 257182.0394444344, 255947.79134919238, 253287.11380951173, 256644.3412698281, 256340.73984126013, 254145.9158730042, 254240.88841268732, 259168.68269840206, 254796.0369047506, 254879.9430158618, 256316.65468252992, 260614.84523808496, 255985.4919047522, 255932.03206348082, 256204.93753967155, 259959.41714284522, 255287.50920633855, 253836.7023809417, 254648.29222221154, 257240.67611109946, 257689.4100793554, 254317.49134919577, 254487.3685714169, 255527.85404760815, 252421.45436506844, 255730.44936506858, 254117.43349205237, 252253.4690476074, 256713.62896824413, 255421.81888887932]}
    episodes = range(1, len(rdata['PPO_Rewards']) + 1)

    # 绘制奖励图
    plt.plot(episodes, rdata['PPO_Rewards'], label='PPO Rewards')
    plt.plot(episodes, rdata['DQN_Rewards'], label='DQN Rewards')

    # 添加标题和标签
    # plt.title('Rewards Comparison')
    plt.xlabel('回合')
    plt.ylabel('奖励')

    # 添加图例
    plt.legend()

    # 保存图像
    timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
    filename = f'rppodqn_{timestamp}.png'
    plt.savefig(filename)

    # 显示图像
    plt.show()
if __name__=='__main__':
    plot_rader()